<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--部门数据-->
            <el-col :span="4" :xs="24">
                <div class="head-container">
                    <el-input
                        v-model="deptName"
                        placeholder="请输入部门名称"
                        clearable
                        size="small"
                        prefix-icon="el-icon-search"
                        style="margin-bottom: 20px"
                    />
                </div>
                <div class="head-container">
                    <el-tree
                        :data="deptOptions"
                        :props="defaultProps"
                        :expand-on-click-node="false"
                        :filter-node-method="filterNode"
                        ref="tree"
                        default-expand-all
                        highlight-current
                        @node-click="handleNodeClick"
                    />
                </div>
            </el-col>

            <el-col :span="20" :xs="24">
                <el-form
                    :model="queryParams"
                    ref="queryForm"
                    size="small"
                    :inline="true"
                    v-show="showSearch"
                    label-width="68px"
                >
                    <el-form-item label="用户姓名" prop="nickName">
                        <el-input
                            v-model="queryParams.signNickName"
                            placeholder="请输入签到/签退。用户昵称"
                            clearable
                            @keyup.enter.native="handleQuery"
                        />
                    </el-form-item>
                    <el-form-item label="年份" prop="year">
                        <el-date-picker
                            type="year"
                            v-model="queryParams.year"
                            :clearable="false"
                            placeholder="选择年"
                            value-format="yyyy"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item label="月份" prop="month">
                        <el-select
                            v-model="queryParams.month"
                            placeholder="请选择月份"
                            clearable
                        >
                            <el-option
                                v-for="month in monthList"
                                :key="month.value"
                                :label="month.label"
                                :value="month.value"
                            />
                        </el-select>
                    </el-form-item>
                    <!--                    <el-form-item label="周" prop="weekDay">-->
                    <!--                        <el-date-picker type="week" v-model="queryParams.weekDays" format="yyyy 第 WW 周" format="WW" placeholder="选择周"></el-date-picker>-->
                    <!--                    </el-form-item>-->
                    <el-form-item>
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            size="mini"
                            @click="handleQuery"
                            >查询</el-button
                        >
                        <el-button
                            icon="el-icon-refresh"
                            size="mini"
                            @click="resetQuery"
                            >重置</el-button
                        >
                    </el-form-item>
                </el-form>

                <TableCard>
                    <el-table
                        :border="Global.tableShowBorder"
                        v-loading="loading"
                        :data="workAttendancesList"
                    >
                        <el-table-column
                            type="selection"
                            width="55"
                            align="center"
                        />
                        <el-table-column
                            label="用户姓名"
                            align="center"
                            prop="nickName"
                        />
                        <el-table-column
                            label="上班打卡正常次数"
                            align="center"
                            prop="inNormalCount"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.inNormalCount == '0'"
                                ></span>
                                <span
                                    v-if="scope.row.inNormalCount != '0'"
                                    style="color: #13ce66; font-weight: 900"
                                    >{{ scope.row.inNormalCount }}</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="迟到次数"
                            align="center"
                            prop="inAbnormalCount"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.inAbnormalCount == '0'"
                                ></span>
                                <span
                                    v-if="scope.row.inAbnormalCount != '0'"
                                    style="color: #f56c6c; font-weight: 900"
                                    >{{ scope.row.inAbnormalCount }}</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="下班打卡正常次数"
                            align="center"
                            prop="outNormalCount"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.outNormalCount == '0'"
                                ></span>
                                <span
                                    v-if="scope.row.outNormalCount != '0'"
                                    style="color: #13ce66; font-weight: 900"
                                    >{{ scope.row.outNormalCount }}</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="早退次数"
                            align="center"
                            prop="outAbnormalCount"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.outAbnormalCount == '0'"
                                ></span>
                                <span
                                    v-if="scope.row.outAbnormalCount != '0'"
                                    style="color: #f56c6c; font-weight: 900"
                                    >{{ scope.row.outAbnormalCount }}</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="上班未打卡次数"
                            align="center"
                            prop="nullInDays"
                        >
                            <template slot-scope="scope">
                                <span v-if="scope.row.nullInDays == '0'"></span>
                                <span
                                    v-if="scope.row.nullInDays != '0'"
                                    style="color: #f56c6c; font-weight: 900"
                                    >{{ scope.row.nullInDays }}</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="下班未打卡次数"
                            align="center"
                            prop="nullOutDays"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.nullOutDays == '0'"
                                ></span>
                                <span
                                    v-if="scope.row.nullOutDays != '0'"
                                    style="color: #f56c6c; font-weight: 900"
                                    >{{ scope.row.nullOutDays }}</span
                                >
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination
                        v-show="total > 0"
                        :total="total"
                        :page.sync="queryParams.pageNum"
                        :limit.sync="queryParams.pageSize"
                        @pagination="getList"
                    />
                </TableCard>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import { statistics } from "@/api/hrm/workAttendances";
import { getUser } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { treeselect } from "@/api/system/dept";
export default {
    name: "WorkAttendances",
    components: { Treeselect },
    data() {
        return {
            // 部门名称
            deptName: undefined,
            // 部门树选项
            deptOptions: [],
            defaultProps: {
                children: "children",
                label: "label",
            },
            monthList: [
                { value: "01", label: "一月" },
                { value: "02", label: "二月" },
                { value: "03", label: "三月" },
                { value: "04", label: "四月" },
                { value: "05", label: "五月" },
                { value: "06", label: "六月" },
                { value: "07", label: "七月" },
                { value: "08", label: "八月" },
                { value: "09", label: "九月" },
                { value: "10", label: "十月" },
                { value: "11", label: "十一月" },
                { value: "12", label: "十二月" },
            ],
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 打卡记录表格数据
            workAttendancesList: [],
            // 遮罩层
            loading: true,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                signUserId: null,
                signNickName: null,
                signInTime: null,
                signOutTime: null,
                realDate: null,
                weekDays: null,
                weekNo: null,

                deptId: null,
                nickName: "",
                month: (new Date().getMonth() + 1 < 10
                    ? "0" + (new Date().getMonth() + 1)
                    : new Date().getMonth() + 1
                ).toString(),
                year: new Date().getFullYear().toString(),
            },
        };
    },
    watch: {
        // 根据名称筛选部门树
        deptName(val) {
            this.$refs.tree.filter(val);
        },
    },
    created() {
        this.getTreeselect();
        this.getList();
    },
    methods: {
        /** 查询打卡记录列表 */
        getList() {
            this.loading = true;
            console.log(this.queryParams);

            if (!this.queryParams.year) {
                this.$modal.msgError("请选择年！");
            }

            statistics(this.queryParams).then((response) => {
                this.workAttendancesList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "oa/workAttendances/export",
                {
                    ...this.queryParams,
                },
                `workAttendances_${new Date().getTime()}.xlsx`
            );
        },

        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
            });
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 节点单击事件
        handleNodeClick(data) {
            this.queryParams.deptId = data.id;
            this.handleQuery();
        },
    },
};
</script>
